/**
 * 
 */
package edu.cmu.cs.webapp.ioms.ui.providers.order;

import java.text.NumberFormat;

import org.eclipse.jface.viewers.ITableColorProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;

import edu.cmu.cs.webapp.ioms.models.LineItem;
import edu.cmu.cs.webapp.ioms.models.PurchaseOrder;
import edu.cmu.cs.webapp.ioms.utils.IOMSImageHelper;
import edu.cmu.cs.webapp.ioms.utils.IOMSImages;

/**
 * @author Christian
 * 
 */
public class ItemsTableViewerLabelProvider extends LabelProvider implements
		ITableLabelProvider, ITableColorProvider {
	// table column index
	public static final int PRODUCT = 0;
	public static final int QUANTITY = 1;
	public static final int UNIT = 2;
	public static final int UNIT_PRICE = 3;
	public static final int GROSS_SALES = 4;
	public static final int REMARKS = 5;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(java.lang
	 * .Object, int)
	 */
	@Override
	public Image getColumnImage(Object element, int columnIndex) {
		if (element instanceof LineItem) {
			if (columnIndex == PRODUCT) {
				return IOMSImageHelper.getInstance().getImage(
						IOMSImages.IMG_PROD);
			}
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(java.lang
	 * .Object, int)
	 */
	@Override
	public String getColumnText(Object element, int columnIndex) {
		if (element instanceof LineItem) {
			LineItem item = (LineItem) element;
			switch (columnIndex) {
			// case ITEM_ID:
			// // id
			// return "#" + item.getItem_id();
			case PRODUCT:
				// product
				return item.getProduct() == null ? "" : item.getProduct()
						.getProd_name();
			case QUANTITY:
				// quantity
				return NumberFormat.getNumberInstance().format(
						item.getQuantity());
			case UNIT:
				// unit
				return item.getUnit();
			case UNIT_PRICE:
				// unit price
				return item.getProduct() == null ? "" : NumberFormat
						.getCurrencyInstance().format(
								item.getProduct().getPrice());
			case GROSS_SALES:
				// gross sale
				return item.getProduct() == null ? "" : NumberFormat
						.getCurrencyInstance().format(
								item.getProduct().getPrice()
										* item.getQuantity());
			case REMARKS:
				return item.getRemark();
			default:
				return "";
			}
		}

		return "";

	}

	@Override
	public Color getBackground(Object element, int columnIndex) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Color getForeground(Object element, int columnIndex) {
		if (element instanceof PurchaseOrder) {
			if (columnIndex == GROSS_SALES) {
				return Display.getDefault().getSystemColor(SWT.COLOR_BLUE);
			}
		}
		return null;
	}
}
